import json
import requests
from faker import Faker
from datetime import datetime

from 接口工具练习.md5签名 import generate_md5_signature, generate_md5

host = 'http://121.11.97.201:9088/'

def test_request_get1():
    url = host + '/pinter/com/getSku?id=1'
    res = requests.get(url=url)
    result = res.text  # 返回的普通数据
    print(result)
    result_json2 = res.json()  # 返回的是字典数据
    print(result_json2)
    print(type(result), type(result_json2))
    assert result_json2['code'] == '0'

def test_request_get2():
    url = host + '/pinter/com/getSku?id=1'
    response = requests.get(url=url)

    # 检查响应状态码
    if response.status_code == 200:
        print("请求成功")
        # 获取响应内容
        data = response.json()  # 假设响应是JSON格式
        print("响应数据:", data)
    else:
        print("请求失败，状态码:", response.status_code)
    rest = response.json()
    assert rest['code'] == '0'
    assert response.status_code == 200

def test_post_test1():
    url = host + '/pinter/com/login'
    params = {
        "userName": "admin",
        "password": "1234"
    }
    response = requests.post(url=url, data=params)
    # 检查响应状态码
    if response.status_code == 200:
        print("请求成功")
        # 获取响应内容
        data = response.json()  # 假设响应是JSON格式
        print("响应数据:", data)
    else:
        print("请求失败，状态码:", response.status_code)
    rest = response.json()
    assert rest['code'] == '0'
    assert response.status_code == 200

def test_post_test2():
    url = host + '/pinter/com/register'
    fake = Faker('zh_CN')
    profile = fake.profile()
    gender = 1 if profile['sex'] == 'M' else 0
    params = {
        "userName": profile['name'],
        "password": "1234",
        "gender": gender,
        "phoneNum": fake.phone_number(),
        "email": profile['mail'],
        "address": profile['address']
    }
    headers = {
        "Content-Type": "application/json"
    }
    response = requests.post(url=url, json=params)  # 默认追加了json头部
    # 检查响应状态码
    if response.status_code == 200:
        print("请求成功")
        # 获取响应内容
        data = response.json()  # 假设响应是JSON格式
        print("响应数据:", data)
    else:
        print("请求失败，状态码:", response.status_code)
    rest = response.json()
    assert rest['code'] == '0'
    assert response.status_code == 200

def test_post_md5():
    url = host + '/pinter/com/userInfo'
    fake = Faker('zh_CN')
    # 生成毫秒级时间戳
    timestamp = int(datetime.now().timestamp() * 1000)
    phone = fake.phone_number()
    #加盐
    to_sign=f'{phone}testfan{timestamp}'
    sign_data= generate_md5(to_sign)
    params = {
        "phoneNum": phone,
        "optCode": "testfan",
        "timestamp": timestamp,
        "sign": sign_data
    }
    response = requests.post(url=url, json=params)  # 默认追加了json头部
    # 检查响应状态码
    if response.status_code == 200:
        print("请求成功")
        # 获取响应内容
        data = response.json()  # 假设响应是JSON格式
        print("响应数据:", data)
    else:
        print("请求失败，状态码:", response.status_code)
    rest = response.json()
    assert response.status_code == 200
    assert rest['code'] == '0'

def test_request_get3():
    resp = requests.get(url="http://www.baidu.com")
    print(resp.text)

def test_request_get4():
    params = {
        "userName": "admin",
        "password": "1234"
    }
    resp = requests.get(url="http://www.baidu.com", params=params)
    print(resp.text)

if __name__ == '__main__':
    # test_request_get1()
    # test_request_get4()
    test_post_md5()
    # test_request_get2()
    # test_request_get3()
    # test_post_test1()
    # test_post_test2()
